package org.wanho.jdbc.util;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.wanho.jdbc.annotation.Column;
import org.wanho.jdbc.annotation.Table;
import org.wanho.rent.entity.SysUser;
import org.wanho.rent.util.CommonUtil;

/**
 * 不需要sql的JDBCTempalte,暂未实现
 */
public class MyJDBCTempalte {
	public static final String INSERT = " INSERT ";
	public static final String UPDATE = " UPDATE ";
	public static final String DELETE = " DELETE ";
	public static final String SELECT = " SELECT ";
	
	public static final String INTO = " INTO ";
	public static final String VALUES = " VALUES ";
	
	public static final String SET = " SET ";
	public static final String WHERE = " WHERE ";
	public static final String FROM = " FROM ";
	public static final String LIMIT = " LIMIT ";
	
	public static final String PROBLEM_MARK = "?";
	public static final String SEMICOLON = ",";
	public static final String BRACKET_LEFT = "(";
	public static final String BRACKET_RIGHT = ")";
	public static final String EQUAL = "=";
	
	public static final String GREATER_THAN = ">";
	public static final String LESS_THAN = "<";
	
	public static final String EMPTY_1 = "";
	public static final String EMPTY_2 = " ";
	public static final String POINT = ".";
	public static final String TABLE_PREFIX = "t_";
	
	
	public MySqlMode getInsertColumnsSql(Object obj){
		MySqlMode sqlMode = getInsertOrSelectColumnsSql(obj, null, false);
		Class<?> c = obj.getClass();
		Table table = c.getAnnotation(Table.class);
		String tableName = table.value();
		if(null == tableName || "".equals(tableName.trim())){
			String simpleName = c.getSimpleName();
			tableName = TABLE_PREFIX + simpleName.substring(0, 1).toLowerCase() + simpleName.substring(1);
		}
		StringBuffer insertSql = new StringBuffer()
				.append(INSERT)
				.append(INTO)
				.append(EMPTY_2)
				.append(tableName)
				.append(BRACKET_LEFT)
				.append(EMPTY_2);
		
		insertSql.append(sqlMode.sql);
		
		insertSql.append(EMPTY_2)
			.append(BRACKET_RIGHT)
			.append(EMPTY_2)
			.append(VALUES)
			.append(EMPTY_2)
			.append(BRACKET_LEFT);
		for (int i = 0; i < sqlMode.params.length; i++) {
			insertSql.append(PROBLEM_MARK);
			if(i < (sqlMode.params.length - 1)){
				insertSql.append(SEMICOLON).append(EMPTY_2);
			}
		}
		insertSql.append(BRACKET_RIGHT);
		sqlMode.sql = insertSql.toString();
		return sqlMode;
	}
	
	public MySqlMode getSelectColumnsSql(Object obj, String tableAliase){
		return getInsertOrSelectColumnsSql(obj, tableAliase, true);
	}
	//查询查所有,添加只加有值的
	private MySqlMode getInsertOrSelectColumnsSql(Object obj, String tableAliase, boolean isSelect){
		MySqlMode msm = new MySqlMode();
		if(null == tableAliase || "".equals(tableAliase.trim())){
			tableAliase = EMPTY_1;
		}else{
			tableAliase = POINT + tableAliase ;
		}
		StringBuffer columnSql = new StringBuffer(EMPTY_2);
		Class<?> c = obj.getClass();
		Field[] fields = c.getDeclaredFields();
		List<Object> params = new ArrayList<Object>();
		if(null != fields){
			for (int i = 0; i < fields.length; i++) {
				Field field = fields[i];
				Column column = field.getAnnotation(Column.class);
				if(null == column){
					continue;
				}
				String columnName = column.value();
				if(null == columnName || "".equals(columnName.trim())){
					columnName = field.getName();
				}
				field.setAccessible(true);
				try {
					Object value = field.get(obj);
					if(isSelect || null != value){
						params.add(value);
						columnSql.append(tableAliase)
						.append(columnName)
						.append(SEMICOLON)
						.append(EMPTY_2);
					}
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
			}
		}
		//去除最后的","
		columnSql.setLength(columnSql.length() - 2);
		columnSql.append(EMPTY_2);
		msm.sql = columnSql.toString();
		msm.params = params.toArray();
		return msm;
	}
	

	public static void main(String[] args) {
		MyJDBCTempalte jt = new MyJDBCTempalte();
//		User user = new User(null, "abc", "123", "12345678901", "/images/Tulips.jpg", new Date());
//		System.out.println(jt.getInsertColumnsSql(user));
//		System.out.println(jt.getSelectColumnsSql(user, ""));
	}
	
//	public <T> void insert(T t){
//		System.out.println(t.getClass());
//	}
//	
//	public <T> void update(T t){
//		System.out.println(t.getClass());
//	}
//	
//	public <T extends Serializable> void delete(T pk, Class<?> clazz){
//		System.out.println(clazz);
//	}
//	
//	public <T> void selectAll(Class<?> clazz){
//		System.out.println(clazz);
//	}
	
	public static class MySqlMode {
		String sql;
		Object[] params;
		@Override
		public String toString() {
			return "MySqlMode {sql=" + sql + "\n params="
					+ Arrays.toString(params) + " }";
		}
	}
	
}
